const express = require("express");
const router = express.Router();
var iconv = require("iconv-lite");
const https = require('https');//https请求的模块
const data = require("./../mock/mock.json");
const multer=require("multer")
const upload = multer({ dest: 'uploads/' });
const fs=require("fs")
const { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } = require("constants");

module.exports = router

//登录接口
router.post("/login", (req, res) => {
    let { userName, passPwd } = req.body;
    let userData = data.userInfo
    let result = userData.find(item => {
        return item.userName === userName && item.passPwd === passPwd;
    })
    if (result != undefined) {
        res.json({
            code: 200,
            msg: "登录成功",
            data: { result: result }
        })
    } else {
        res.json({
            code: 404,
            msg: "用户名或密码错误",
            data: ""
        })

    }
})


//获取团队数据
router.get("/getGroupData", (req, res) => {
    var arr = data.groupData
    var { page, num, groupName, time } = req.query
    // console.log(Number(page))
    // console.log(Number(num))
    // console.log(groupName)
    // console.log(time)
    var serchResult = []
    var result = []
    var count = arr.length

    if (groupName != "") {
        console.log("not null")
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].contractTeam.indexOf(groupName) >= 0) {
                serchResult.push(arr[i])
            }
        }
        count = serchResult.length
    }

    if (time != undefined) {
        let temp = []
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].contactDate >= time[0] && arr[i].contactDate <= time[1]) {
                temp.push(arr[i])
            }
        }
        if (groupName != "") {
            serchResult = []
            for (let i = 0; i < temp.length; i++) {
                if (temp[i].contractTeam.indexOf(groupName) >= 0) {
                    serchResult.push(temp[i])
                }
            }
        } else {
            serchResult = temp
        }

        count = serchResult.length
    }

    var start = Number(num) * (Number(page) - 1)
    var end = Number(num) * (Number(page) - 1) + Number(num)

    if (page == parseInt(count / num) + 1) {
        end = count - (Number(page) - 1) * Number(num) + start
    }
    for (var i = start; i < end; i++) {
        if (groupName != "" || time != undefined) {
            result.push(serchResult[i])
        } else {
            result.push(arr[i])
        }

        // console.log("result:"+i)
    }
    // console.log(start)
    // console.log(end)
    // console.log("result!!!!!!!!:")
    // console.log(result)
    res.json({
        code: 200,
        msg: "请求团队数据成功",
        data: result,
        count: count
    })
})


//每个月的签约数量
router.get("/getMonNum", (req, res) => {
    var January = 0
    var February = 0
    var March = 0
    var April = 0
    var May = 0
    var June = 0
    var July = 0
    var August = 0
    var September = 0
    var October = 0
    var November = 0
    var December = 0




    for (let i = 0; i < data.groupData.length; i++) {
        // console.log(data.groupData[i].contactDate)
        var date = new Date(data.groupData[i].contactDate)
        var mon = date.getMonth() + 1

        switch (mon) {
            case 1: January++; break;
            case 2: February++; break;
            case 3: March++; break;
            case 4: April++; break;
            case 5: May++; break;
            case 6: June++; break;
            case 7: July++; break;
            case 8: August++; break;
            case 9: September++; break;
            case 10: October++; break;
            case 11: November++; break;
            case 12: December++; break;
        }
        // console.log(mon)

    }

    result = { "January": January, "February": February, "March": March, "April": "April", "April": April, "May": May, "June": June, "July": July, "August:": August, "September": September, "October": October, "November": November, "December:": December }
    res.json({
        code: 200,
        msg: "查询月份签约数量成功",
        data: result
    })
})


//居民类型数量
router.get("/getPersonNum", (req, res) => {
    var children = 0
    var elderly = 0
    var maternal = 0
    var hypertension = 0
    var diabetes = 0
    var disorder = 0
    var disabilitie = 0
    var healthyPeople = 0

    for (let i = 0; i < data.groupData.length; i++) {
        children += data.groupData[i].children
        elderly += data.groupData[i].elderly
        maternal += data.groupData[i].maternal
        hypertension += data.groupData[i].hypertension
        diabetes += data.groupData[i].diabetes
        disorder += data.groupData[i].disorder
        disabilitie += data.groupData[i].disabilitie
        healthyPeople += data.groupData[i].healthyPeople
    }
    // var result=[children,elderly,maternal,hypertension,diabetes,disorder,disabilitie,healthyPeople]
    var result = [
        { "value": children, "name": "0~6岁儿童" },
        { "value": elderly, "name": "老年人" },
        { "value": maternal, "name": "孕产妇" },
        { "value": hypertension, "name": "高血压" },
        { "value": diabetes, "name": "糖尿病" },
        { "value": disorder, "name": "严重精神障碍" },
        { "value": disabilitie, "name": "残疾人" },
        { "value": healthyPeople, "name": "健康居民" }
    ]
    res.json({
        code: 200,
        msg: "获取居民类型统计数量成功",
        data: result
    })
})

//团队的签约数量排名前十的团队，以及数量
router.get("/getQianYueNum",(req,res)=>{
    var arr = data.groupData
    let result=arr.find(item=>{
        return item.contractNumber=100
    })
    let temp=[]
    let obj={}
    for(let i=0;i<arr.length;i++){
        obj.contractTeam=arr[i].contractTeam
        obj.contractNumber=arr[i].contractNumber
        temp.push(obj)
        obj={}
    }
    // console.log(temp)

    function sortNum(a,b){
        return b.contractNumber-a.contractNumber
    }
    temp.sort(sortNum)
    // console.log(temp)

    
    // console.log(temp.slice(1,10))
    res.json({
        code:200,
        msg:"查询成功",
        data:temp.slice(0,10)
    })
})



//获取家庭医生团队列表
router.get("/getTeamData", (req, res) => {
    var arr = data.doctorTeams
    var { page, num, groupName, doctor } = req.query


    var serchResult = []
    var result = []
    var count = arr.length

    if (groupName != "") {
        // console.log("not null")
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].groupName.indexOf(groupName) >= 0) {
                serchResult.push(arr[i])
            }
        }
        count = serchResult.length
    }

    if (doctor != "") {
        let temp = []
        for (let i = 0; i < arr.length; i++) {
            for (let j = 0; j < arr[i].teamItems.length; j++) {
                if (arr[i].teamItems[j].manager === 1 && arr[i].teamItems[j].d_name.indexOf(doctor) >= 0) {
                    temp.push(arr[i])
                }
            }
        }
        if (groupName != "") {
            serchResult = []
            for (let i = 0; i < temp.length; i++) {
                if (temp[i].groupName.indexOf(groupName) >= 0) {
                    serchResult.push(temp[i])
                }
            }
        } else {
            serchResult = temp
        }

        count = serchResult.length
    }

    var start = Number(num) * (Number(page) - 1)
    var end = Number(num) * (Number(page) - 1) + Number(num)

    if (page == parseInt(count / num) + 1) {
        end = count - (Number(page) - 1) * Number(num) + start
    }
    for (var i = start; i < end; i++) {
        if (groupName != "" || doctor != "") {
            result.push(serchResult[i])
        } else {
            result.push(arr[i])
        }

        // console.log("result:"+i)
    }


    res.json({
        code: 200,
        msg: "获取家庭医生团队列表成功",
        data: result,
        count: count
    })
})


//删除家庭医生团队列表的某个数据
router.post("/deleteTeam", (req, res) => {
    var arr = data.doctorTeams
    let { tid } = req.body
    // console.log(req.body)
    let result = arr.find(item => {
        return item.tid === tid
    })
    // console.log(result)
    var index = arr.indexOf(result)
    //  console.log(index)
    arr.splice(index, 1)
    res.json({
        code: 200,
        msg: "删除成功"
    })
})

//查询某个团队信息
router.post("/getTeamData", (req, res) => {
    var arr1 = data.doctorTeams
    var arr2 = data.doctorArr
    var arr3 = data.logoUrl
    // console.log(req.body)
    let { tid } = req.body
    // console.log(tid)
    let result = arr1.find(item => {
        return item.tid === tid
    })
    console.log(result)
    let imgUrlResult = arr3.find(item => {
        return item.tid === tid
    })
     console.log(imgUrlResult)
    result.imgUrl = imgUrlResult.imgUrl
    let temp = []
    for (let i = 0; i < arr2.length; i++) {
        for (let j = 0; j < result.teamItems.length; j++) {
            if (arr2[i].d_id === result.teamItems[j].d_id) {
                temp.push(arr2[i])
            }
        }
    }
    for (let i = 0; i < temp.length; i++) {
        for (let j = 0; j < result.teamItems.length; j++) {
            if (temp[i].d_id === result.teamItems[j].d_id) {
                temp[i].manager = result.teamItems[j].manager
            }
        }
    }

    console.log(temp)
    res.json({
        code: 200,
        msg: "获取该团队信息成功",
        data: { "doctorTeams": result, "doctorArr": temp, "allDoctor": arr2 }
    })
})


//获取所有医生列表
router.get("/getAllDoctor",(req,res)=>{
    var arr = data.doctorArr
    res.json({
        code:200,
        msg:"查询成功",
        data:arr
    })
})

//修改某个团队信息
router.post("/editTeamData", (req, res) => {
    var arr1 = data.doctorTeams

    let { tid, groupName, imgUrl, teamInfo, teamItems } = req.body
    // console.log(req.body)

    let result = arr1.find(item => {
        return item.tid === tid
    })
    console.log(req.body)

    result.groupName = groupName
    result.imgUrl = imgUrl
    result.teamInfo = teamInfo
    result.teamItems = teamItems

    res.json({
        code: 200,
        msg: "修改成功"
    })


})

//添加团队信息
router.post("/addTeamData",(req,res)=>{
    var arr1 = data.doctorTeams

    let { tid, groupName, imgUrl, teamInfo, teamItems } = req.body
    // console.log(req.body)
    let obj={}
    obj.tid=arr1.length+1
    obj.groupName=groupName
    obj.imgUrl="logo.png"
    obj.teamInfo=teamInfo
    obj.teamItems=teamItems
    console.log(teamItems)
    arr1.push(obj)

    res.json({
        code:200,
        msg:"添加成功",
        data:arr1
    })
})


//删除团队成员
router.post("/deleteTeamMember", (req, res) => {
    var arr = data.doctorArr
    let { d_id } = req.body
    let result = arr.find(item => {
        return item.d_id === d_id
    })
    var index = arr.indexOf(result)
    arr.splice(index, 1)
    res.json({
        code: 200,
        msg: "删除成功",
        data:arr
    })
})

//添加团队成员
router.post("/addMember", (req, res) => {
    var arr1 = data.doctorTeams
    var arr2 = data.doctorArr
    var isTrue = true
    let { tid, d_id } = req.body
    console.log(d_id)
    let result = arr1.find(item => {
        return item.tid === tid
    })
    console.log(result)
    let result1 = arr2.find(item => {
        return item.d_id === d_id
    })
    for (let i = 0; i < result.teamItems.length; i++) {
        console.log(result.teamItems[i].d_id === d_id)
        if (result.teamItems[i].d_id === d_id) {
            isTrue = false
        }
    }
    let temp = {}
    temp.d_id = result1.d_id
    temp.d_name = result1.d_name
    temp.manager = 0

    if (isTrue == true) {
        result.teamItems.push(temp)
        res.json({
            code: 200,
            msg: "添加成功",
            data:result.teamItems
        })
    } else {
        res.json({
            code: 400,
            msg: "该成员已存在"
        })

    }
})


//面试题测试
router.get("/ajax.aspx", (req, res) => {
    let { type } = req.query
    console.log(req.query)
    let arr = data.ul
    console.log(arr[0].list)
    let result = arr.find(item => {
        return item.src === type && item.list != undefined
    })
    console.log(result)
    if (result != undefined) {
        res.json({
            code: 200,
            msg: "获取成功",
            data: result
        })
    }
})


//获取家庭医生
router.get("/getDoctor", (req, res) => {
    var arr = data.doctorArr
    var { page, num, doctorName, job } = req.query
    // console.log(page)
    // console.log(num)
    // console.log(doctorName)
    // console.log(job)

    var serchResult = []
    var result = []
    var count = arr.length

    if (doctorName != "") {
        // console.log("not null")
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].d_name.indexOf(doctorName) >= 0) {
                serchResult.push(arr[i])
            }
        }
        count = serchResult.length
    }




    if (job != "") {
        let temp = []
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].d_join == job) {
                temp.push(arr[i])
            }
        }
        if (doctorName != "") {
            serchResult = []
            for (let i = 0; i < temp.length; i++) {
                if (temp[i].d_name.indexOf(doctorName) >= 0) {
                    serchResult.push(temp[i])
                }
            }
        } else {
            serchResult = temp
        }

        count = serchResult.length
    }




    var start = Number(num) * (Number(page) - 1)
    var end = Number(num) * (Number(page) - 1) + Number(num)

    if (page == parseInt(count / num) + 1) {
        end = count - (Number(page) - 1) * Number(num) + start
    }
    for (var i = start; i < end; i++) {
        if (doctorName != "" || job != "") {
            result.push(serchResult[i])
        } else {
            result.push(arr[i])
        }

        // console.log("result:"+i)
    }
    // console.log(start)
    // console.log(end)
    // console.log("result!!!!!!!!:")
    // console.log(result)
    res.json({
        code: 200,
        msg: "请求团队数据成功",
        data: result,
        count: count
    })
})

//删除家庭医生某个医生
router.post("/delDoctor",(req,res)=>{
    let {d_id}=req.body

    var arr = data.doctorArr

    let result=arr.find(item=>{
        return item.d_id==d_id
    })

    var index = arr.indexOf(result)
    arr.splice(index, 1)
    res.json({
        code: 200,
        msg: "删除成功"
    })
})

//查询某个家庭医生信息
router.post("/getDoctorData",(req,res)=>{
    let arr=data.doctorArr
    let {d_id}=req.body


    // console.log(d_id)
    let result=arr.find(item=>{
        return item.d_id==d_id
    })

    res.json({
        code:200,
        msg:"查询成功",
        data:result
    })
})

//修改某个家庭医生信息
router.post("/editDoctorData",(req,res)=>{
    console.log(req.body)
    let {d_id,d_name,d_sex,d_imgUrl,d_sid,d_tel,d_join,d_feature}=req.body
    let arr=data.doctorArr

    let result=arr.find(item=>{
        return item.d_id==d_id
    })

    result.d_name=d_name
    result.d_sex=d_sex
    result.d_imgUrl=d_imgUrl
    result.d_sid=d_sid
    result.d_tel=d_tel
    result.d_join=d_join
    result.d_feature=d_feature

    res.json({
        code:200,
        msg:"修改成功"
    })
})

//获取签约居民列表
router.get("/getContractManager", (req, res) => {
    var arr = data.contractData
    var { page, num, doctorName, status } = req.query
    // console.log(page)
    // console.log(num)
    // console.log(doctorName)
    // console.log(status)


    var serchResult = []
    var result = []
    var count = arr.length

    if (doctorName != "") {
        // console.log("not null")
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].c_doctorManager.indexOf(doctorName) >= 0) {
                serchResult.push(arr[i])
            }
        }
        count = serchResult.length
    }




    if (status != "") {
        let temp = []
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].c_contractState == status) {
                temp.push(arr[i])
            }
        }
        if (doctorName != "") {
            serchResult = []
            for (let i = 0; i < temp.length; i++) {
                if (temp[i].d_name.indexOf(doctorName) >= 0) {
                    serchResult.push(temp[i])
                }
            }
        } else {
            serchResult = temp
        }

        count = serchResult.length
    }




    var start = Number(num) * (Number(page) - 1)
    var end = Number(num) * (Number(page) - 1) + Number(num)

    if (page == parseInt(count / num) + 1) {
        end = count - (Number(page) - 1) * Number(num) + start
    }
    for (var i = start; i < end; i++) {
        if (doctorName != "" || status != "") {
            result.push(serchResult[i])
        } else {
            result.push(arr[i])
        }

        // console.log("result:"+i)
    }
    // console.log(start)
    // console.log(end)
    // console.log("result!!!!!!!!:")
    // console.log(result)
    res.json({
        code: 200,
        msg: "请求数据成功",
        data: result,
        count: count
    })
})

//删除签约居民列表
router.post("/delContractManager", (req, res) => {
    var arr = data.contractData
    var { c_id } = req.body
    console.log(c_id)

    let result = arr.find(item => {
        return item.c_id === c_id
    })
    // console.log(result)
    var index = arr.indexOf(result)
    //  console.log(index)
    arr.splice(index, 1)
    res.json({
        code: 200,
        msg: "删除成功"
    })
})

//查询某个签约居民信息
router.post("/getContractData",(req,res)=>{
    let {c_id}=req.body
    let arr=data.contractData
// console.log(c_id)
    let result = arr.find(item => {
        return item.c_id === c_id
    })
    console.log(result)
    res.json({
        code: 200,
        msg: "查询成功",
        data:result
    })
})

//修改某个签约居民信息
router.post("/editContract",(req,res)=>{
    let {c_id,c_name,c_sex,c_imgUrl,c_address,c_sid,c_tel,c_type,c_teamManager,c_doctorManager,c_contractState,serverPack}=req.body
    let arr=data.contractData

    let result = arr.find(item => {
        return item.c_id === c_id
    })
    result.c_name=c_name
    result.c_sex=c_sex
    result.c_imgUrl=c_imgUrl
    result.c_address=c_address
    result.c_sid=c_sid
    result.c_tel=c_tel
    result.c_type=c_type
    result.c_teamManager=c_teamManager
    result.c_doctorManager=c_doctorManager
    result.c_contractState=c_contractState
    result.serverPack=serverPack

    res.json({
        code:200,
        msg:"修改成功"
    })
})